﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EGS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SchoolController : ControllerBase
    {
        public readonly EGSDbContext db;
        public SchoolController(EGSDbContext context)
        {
            db = context;
        }

        public List<EGS_School> GetAll()
        {
            return db.School.ToList();
        }

        [HttpGet]
        public List<EGS_School> GetAll([FromBody] string SchoolProfile,string LeadershipTeam,string LeadingOrganizationalStructure,string HonorAndQualification)
        {
            IQueryable<EGS_School> query = db.School;
            if (!string.IsNullOrEmpty(SchoolProfile))
            {
                query = query.Where(s => s.SchoolProfile == SchoolProfile);
            }
            if (!string.IsNullOrEmpty(LeadershipTeam))
            {
                query = query.Where(s => s.LeadershipTeam == LeadershipTeam);
            }
            if (!string.IsNullOrEmpty(LeadingOrganizationalStructure))
            {
                query = query.Where(s => s.LeadingOrganizationalStructure == LeadingOrganizationalStructure);
            }
            if (!string.IsNullOrEmpty(HonorAndQualification))
            {
                query = query.Where(s => s.HonorAndQualification == HonorAndQualification);
            }
            return query.ToList();
        }

        [HttpGet]
        public List<EGS_School> GetBySelect([FromBody] string SchoolProfile, string LeadershipTeam, string LeadingOrganizationalStructure, string HonorAndQualification)
        {
            List<EGS_School> School = new List<EGS_School>();
            if (!string.IsNullOrEmpty(SchoolProfile))
            {
                School = db.School.Where(s => s.SchoolProfile == SchoolProfile).ToList();
            }
            if (!string.IsNullOrEmpty(LeadershipTeam))
            {
                School = db.School.Where(s => s.LeadershipTeam == LeadershipTeam).ToList();
            }
            if (!string.IsNullOrEmpty(LeadingOrganizationalStructure))
            {
                School = db.School.Where(s => s.LeadingOrganizationalStructure == LeadingOrganizationalStructure).ToList();
            }
            if (!string.IsNullOrEmpty(HonorAndQualification))
            {
                School = db.School.Where(s => s.HonorAndQualification == HonorAndQualification).ToList();
            }
            return School;
        }

            /// <summary>
            /// 查
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            [HttpGet("{id}")]
            public EGS_School? GetEGS_SchoolInfoById(Guid id)
            {
                EGS_School? School = new EGS_School();
                School = db.School.Where(s => s.ID == id).FirstOrDefault();
                return School;
            }
        [HttpPost]
        public Result AddSchool([FromBody] EGS_School school)
        {
            Result result = new Result();
            db.School.Add(school);
            result.Data = db.SaveChanges();
            return result;
        }
        [HttpDelete]
        public Result DelSchool(Guid Id)
        {
            Result? result = new Result();
            EGS_School? School = new EGS_School();
            School = db.School.Where(s => s.ID == Id).FirstOrDefault();
            return result;
        }
    }
}

